/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "NodeCpp/Data/Rel/Details/MySQLConnection.h"
#include "NodeCpp/Data/Rel/Session.h"
#include "NodeCpp/Data/Rel/Details/MySQLQueryResult.h"

namespace NodeCpp {
namespace Data {
namespace Rel {

    MySQLConnection::MySQLConnection(Session* _Sess)
        : SqlConnection(_Sess)
        , Res_(NULL)
    {

    }

    MySQLConnection::~MySQLConnection(void)
    {

    }

    void MySQLConnection::Close(void)
    {
        if (Res_ != NULL)
        {
            mysql_close(Res_);
            Res_ = NULL;
        }
    }

    void MySQLConnection::EscapeString(std::string& _Sql)
    {
        char _Lsql[Session::MAX_QUERY_LENGTH];
        mysql_escape_string(_Lsql, _Sql.c_str(), (unsigned long)_Sql.length());
        _Sql.clear();
        _Sql = _Lsql;
    }

    void MySQLConnection::Ping(void)
    {
        NODECPP_ASSERT(Res_);
        SqlConnection::Lock _Guard(this);
        if (mysql_ping(Res_) != 0)
        {
            _HandleError();
            return;
        }
        _ResetError();
    }

    std::string MySQLConnection::GetConnectionInfo(void)
    {
        return std::string();
    }

    std::string MySQLConnection::GetServerInfo(void)
    {
        return std::string(mysql_get_server_info(Res_));
    }

    std::string MySQLConnection::GetClientInfo(void)
    {
        return std::string(mysql_get_client_info());
    }

    QueryResult *MySQLConnection::Query(const std::string &_Sql)
    {
        if (!mysql_query(Res_, _Sql.c_str()))
        {
            _ResetError();
            MYSQL_RES* _Res = mysql_store_result(Res_);
            if (_Res != NULL)
            {
                AutoPtr<QueryResult> _Rs(new MySQLQueryResult(_Res,
                    mysql_affected_rows(Res_)));
                if (_Rs.get() && _Rs->NextRow()) {
                    return _Rs.release();
                }
            }
            return NULL;
        }
        _HandleError();
        return NULL;
    }

    bool MySQLConnection::Execute(const std::string &_Sql)
    {
        if (mysql_query(Res_, _Sql.c_str())) {
            _HandleError();
            return false;
        }
        _ResetError();
        return true;
    }

    bool MySQLConnection::BeginTransaction(void)
    {
        return _TransactionCmd("START TRANSACTION");
    }

    bool MySQLConnection::CommitTransaction(void)
    {
        return _TransactionCmd("COMMIT");
    }

    bool MySQLConnection::RollbackTransaction(void)
    {
        return _TransactionCmd("ROLLBACK");
    }

    bool MySQLConnection::_TransactionCmd(const char *_Cmd)
    {
        if (mysql_query(Res_, _Cmd))
        {
            _HandleError();
            return false;
        }
        _ResetError();
        return true;
    }

    void MySQLConnection::_HandleError(void)
    {
        Error_ = true;
        ErrCode_ = mysql_errno(Res_);
        ErrString_ = mysql_error(Res_);
        _ReportError();
    }

    void MySQLConnection::_ResetError(void)
    {
        Error_ = false;
    }
}}}